require(['init'], function () {
  var $pagination = $('#js-pagination')
  var pagination
  var status;
  $("#course_selected").on('change',function(){
  status=$('#course_selected option:selected').attr('data-uid');
  })
  console.log(status)
  function getBannerList(querystr, cb) {
    var url = '/api/products';
    var query = {}
    if(querystr) {
      query = querystr
    }
    ajaxMethod({
      url: url,
      data: query,
      success: function (data) {
        cb && cb(data)
        if(data.list.length == 0) {
          $('#course_list').html('<div style="margin-top: 50px;text-align: center;">'+i18next.t('input.nullresults')+'</div>');
          $pagination.hide()
          return;
        }
        var $listTable = $('#course_list').html('');
        $.ajax({
          url:'/api/coursetypes',
          method:'get',
          success:function(json){
          $thead = $('<thead>\
          <tr>\
            <th>'+i18next.t('table.no')+'</th>\
            <th>'+i18next.t('table.productname')+'</th>\
            <th>'+i18next.t('table.lectureteacher')+'</th>\
            <th>'+i18next.t('table.productclassnumber')+'</th>\
            <th>'+i18next.t('table.coursetype')+'</th>\
            <th>'+i18next.t('table.producttype')+'</th>\
            <th>'+i18next.t('table.createrinfo')+'</th>\
            <th>'+i18next.t('table.issuetime')+'</th>\
            <th>'+i18next.t('table.cancleissue')+'</th>\
            <th>'+i18next.t('table.productstatus')+'</th>\
            <th>'+i18next.t('table.handle')+'</th>\
          </tr>\
        </thead>').appendTo($listTable)
        var $tbody = $('<tbody></tbody>').appendTo($listTable)
        $.each(data.list, function (i, item) {
          //console.log(item)
          var $item = $('<tr></tr>')
          $item.append('<td>'+ (i+1) +'</td>')
          $item.append('<td>'+ (item.title || '') +'</td>')
          var $tdlast=$('<td>'+(item.masterinfo==null?"":item.masterinfo.lastname)+'</td>').appendTo($item)
          $item.append('<td>'+ (item.duration||'') +'</td>')
          var coursetype_name=$('<td></td>').appendTo($item);  //课程类型
          $item.append('<td>'+ (item.type==1?i18next.t('course.classclass'):i18next.t('course.aboutclass')) +'</td>')  //产品类型
          $item.append('<td>'+ (item.created_by||'') +'</td>')
          var $currenttime=$('<td></td>').appendTo($item)//发布时间
          var $nocurrenttime=$('<td></td>').appendTo($item)//发布时间
          var $status=$('<td class="release"></td>').appendTo($item)
          var $handle = $('<td></td>').appendTo($item); //操作
          $.each(json.data,function(index,it){
            if(item.coursetype==it.coursetypeuid){
              var $coursetype_aneme=$('<a>'+(it.coursetypename||'')+'</a>').appendTo(coursetype_name)
            }
          })
          var masterteacher=$tdlast.text();
          var coursertype=coursetype_name.text();
          var $edit = $('<a class="btn btn-primary btn-sm btn-inverse ml5" href="course.html?id='+item.id+'&masterteacher='+masterteacher+'&coursertype='+coursertype+'">'+i18next.t('course.compile')+'</a>').appendTo($handle);
          var $addClass = $('<a class="btn btn-primary btn-sm btn-inverse ml5" style="color:#398dee" href="product_class.html?id='+item.id+'">'+i18next.t('course.classmanagement')+'</a>').appendTo($handle);
          var $job = $('<span></span>').appendTo($handle);
          if(item.status=="1"){
            //var unpublishtime=$.toDate(item.unpublishtime).Format('YYYY-MM-DD hh:mm:ss')
            $('<a>'+(item.unpublishtime==null?"--":item.unpublishtime)+'</a>').appendTo($nocurrenttime);
            $('<a>'+(item.publishtime==null?"--":item.publishtime)+'</a>').appendTo($currenttime);
            $('<a>'+i18next.t('course.noissue')+'</a>').appendTo( $status);
            $('<a class="btn btn-primary btn-sm btn-inverse ml5">'+i18next.t('course.issuecourse')+'</a>').appendTo( $job);
            $job.on('click',function(){
              var popUpBox = new PopUpBox({
                  type: 'dom',
                  style: 'default contentScroll',
                  submitBtnType: 'confirm',
                  keySubmit: false,
                  titleText: i18next.t('popupbox.pleaseinfo'),
                  show: true,
                  content: "<p style='margin-left:20px;'>确定发布该课程吗？</p>",
                  callback: {
                    yes: function (e) {
                      ajaxMethod({
                       url:'/api/product/'+item.id+'/publish',
                       method:'put',
                       success:function(data,status){
                         if(status.code!=1){
                           window.location.reload();
                         }else{
                            _Tips(status.message)
                         }

                       }
                     })
                    }
                  },
                  position: {
                      width: 500,
                      height: 200
                  }
              })
            })
          }else if(item.status=="2"){
            $('<a>'+(item.publishtime==null?"--":item.publishtime)+'</a>').appendTo($currenttime);
            $('<a>'+(item.unpublishtime==null?"--":item.unpublishtime)+'</a>').appendTo($nocurrenttime);
            //var publishtime=$.toDate(item.publishtime).Format('YYYY-MM-DD hh:mm:ss')
            //$('<a>'+publishtime+'</a>').appendTo($currenttime);
            $('<a class="btn btn-primary btn-sm btn-inverse ml5">'+i18next.t('course.closecourse')+'</a>').appendTo( $job);
            $('<a>'+i18next.t('course.issue')+'</a>').appendTo( $status);
            $job.on('click',function(){
              var popUpBox = new PopUpBox({
                  type: 'dom',
                  style: 'default contentScroll',
                  submitBtnType: 'confirm',
                  keySubmit: false,
                  titleText: i18next.t('popupbox.pleaseinfo'),
                  show: true,
                  content: "<p style='margin-left:20px;'>确定发布该课程吗？</p>",
                  callback: {
                    yes: function (e) {
                      ajaxMethod({
                        url:'/api/product/'+item.id+'/unpublish',
                        method:'put',
                        success:function(data,status){
                          if(status.code!=1){
                            window.location.reload();
                          }else{
                             _Tips(status.message)
                          }
                        }
                      })
                    }
                  },
                  position: {
                      width: 500,
                      height: 200
                  }
              })

             })
          }else{
              $('<a>'+i18next.t('course.closed')+'</a>').appendTo( $status);
          }
          $tbody.append($item)
        })
        $pagination.show()
        }
      })
      }
    })
  }
  //搜索
  function loadSearchData(querystr) {
    if(querystr) {
      pagination.filter.title = querystr
      pagination.searchData()
    } else {
      pagination.reloadData()
    }
  }

  $('#J_course_product').on('click', function() {
    var $input = $(this).prev();
    var querystr = $.trim($input.val());
    loadSearchData(querystr)
  })
  $('.searchInput').on('keyup', function(e) {
    if(e.keyCode == 13) {
      var querystr =$.trim($(this).val());
      loadSearchData(querystr)
    }
  })

 pagination = new Pagination($pagination, getBannerList)
})
